package pe.com.csi.pm.regexiv;

import java.util.ArrayList;
import java.util.List;

import pe.com.csi.pm.regexiv.bean.Tramo;
import pe.com.csi.pm.regexiv.service.TramoService;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;

public class SeleccionTramo extends Activity {

	private ImageButton btnSeleccion;
	private Spinner cmbTramo;
	private TramoService tramoService;
	private Intent intent;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.seleccion_tramo);
		tramoService = new TramoService(getApplicationContext());
		cmbTramo = (Spinner) findViewById(R.id.cmbTramo);
		btnSeleccion = (ImageButton) findViewById(R.id.btnSeleccion);
		cargarTramo();

		btnSeleccion.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {

				if (seleccionarTramo()) {
					intent = new Intent(SeleccionTramo.this, MenuTipoEV.class);
					startActivity(intent);
				}

			}
		});

	}

	public void cargarTramo() {
		List<Tramo> lstramos = new ArrayList<Tramo>();
		lstramos.addAll(tramoService.obtenerTramos());
		if (lstramos != null && lstramos.size() > 0) {
			ArrayAdapter<Tramo> adaptador = new ArrayAdapter<Tramo>(this, android.R.layout.simple_spinner_item, lstramos);
			adaptador.setDropDownViewResource(android.R.layout.simple_spinner_item);
			cmbTramo.setAdapter(adaptador);
			cmbTramo.setSelection(0);
		} else {
			showToast(this, "No existen tramos a cargar");
			intent = new Intent(SeleccionTramo.this, MenuPrincipal.class);
			startActivity(intent);
		}

	}

	private boolean seleccionarTramo() {
		boolean resultado = false;
		Tramo itemtramo = (Tramo) cmbTramo.getSelectedItem();

		SharedPreferences sp = getSharedPreferences("TRAMO", MODE_PRIVATE);

		if (itemtramo != null) {
			sp.edit().putString("TRAMO", String.valueOf(itemtramo.getIdTmo())).commit();
			sp.edit().putString("IDRUTA", String.valueOf(itemtramo.getNumeroRta())).commit();
			sp.edit().putString("IDCCES", String.valueOf(itemtramo.getIdCces())).commit();
			sp.edit().putString("PROGI", String.valueOf(itemtramo.getpInicialTmo())).commit();
			sp.edit().putString("PROGF", String.valueOf(itemtramo.getpFinalTmo())).commit();
			resultado = true;
		}
		return resultado;
	}

	private void showToast(Context mContext, String text) {
		Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();
	}
}
